<?php
/**
 *
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package
 * @subpackage
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * log file in w3c format
 * @property array $fields fields, in the order in which they  will appear
 * @property $available_fields
 */
class WakServerLogFile extends WakLogFile {

    /**
     *
     * @param
     * /
    public function __construct() {

    }/**/

    /* public methods, then properties */

    /**
     * factory : get a day's log instance
     * @param string $path logs folder
     * @param string $basename defaults to empty string (so file will be logs_folder/yyyy-mm-dd.log )
     * @param string $date date yyyy-mm-dd
     * @return WakLogFile
     */
    public static function getForDay($path, $basename='', $date=null) {
        if (!is_string($date))
            $date = date('Y-m-d');
        if (is_string($basename) and $basename)
            $path .= $basename.'.';
        return new WakServerLogFile($path.$date.'.log');
    }

    /**
     * sets logs fields
     * @param array $fields available fields are

    /**
     * add a line with provided data AND auto values for log-defined fields
     * @param array $data associative array field => value
     * @return bool
     */
    public function addEntry($data=array()) {
        // validating / generating data line
        // print_r($this->fields);
        foreach ($this->fields as $field) {
            if (array_key_exists($field, $data))
                $data[$field] = $this->_build_field($field, $data[$field]);
            else
                $data[$field] = $this->_build_field($field);
        }
        // building and writing line
        // print_r($data);
        $line = $this->_buildLine($data); // echo $line;
        return $this->_writeLogLine($line);
    }

    public $fields = array(
        'time'
        , 'c-ip'
        , 'cs-method'
        , 'cs-uri-stem'
        , 'cs-uri-query'
    );

    public static $available_fields = array(
        'date', // yyyy-mm-dd
        'time', // HH:ii:ss
        'time-taken', // float, seconds
        'bytes', // integer, bytes transferred
        'cached', // 0|1
        'c-ip', // ip du client
        's-status', // code de reponse http
        'cs-method', // GET|POST
        'cs-uri', // chaine de requete complete
        'cs-uri-stem', // uri sans variables
        'cs-uri-query', // variables GET
        's-comment', // commentaire
    );

    /* private methods, then properties */

    /**
     * checks or generate value for a field
     * will search for a _field_{key}($value) method
     * key hyphens (-) will converted to underscores (_)
     * @param $key field key (time, c-ip, cs-uri, etc)
     * @param $value if null may try to generate it
     * @return string|false
     */
    protected function _build_field($key, $value=null) {
        $method = '_field_'.str_replace('-','_',strtolower($key));
        if (!method_exists($this,$method)) {
            throw new WakNotImplemented("field $key is not supported by this log");
        }
        return $this->$method($value);
    }

    /* fields methods */

    protected function field_date($value=null) {
        if (is_string($value) and ereg('[0-9-]{10}',$value))
            return $value;
        return date('Y-m-d');
    }
    protected function _field_time($value=null) {
        return date('H:i:s');
    }
    function _field_c_ip($value=null) {
        return $_SERVER['REMOTE_ADDR'];
    }
    function _field_cs_method($value=null) {
        return strtoupper($_SERVER['REQUEST_METHOD']);
    }
    function _field_cs_uri($value=null) {
        return $_SERVER['REQUEST_URI'];
    }
    function _field_cs_uri_stem($value=null) {
        return substr($_SERVER['REQUEST_URI'],0,strpos($_SERVER['REQUEST_URI'],'?'));
    }
    function _field_cs_uri_query($value=null) {
        return substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],'?'));
    }
}